// pages/partnercenter/fansshare/fansshare.js
var app = getApp()
var sceneres = {}
var shihan = require('../../../common/fetch')
var config = require('../../../common/config')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    page_size: 20,
    favoritelist: [],
    dialogflag: true,
    favoritelistflag: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) { //partner_product_rem
    console.log(options)
    var that = this
    if (options.pid) {
      sceneres['stage'] = 1;
      sceneres['partner_id'] = options.pid
      this.setData({
        partner_id: options.pid
      })
    }
    console.log(sceneres)
    app.wechatSwitch().then(function(wechat_switch) { //是否打开微信授权开关
      app.commonLogin(sceneres, wechat_switch)
    })
  },
  viewgoodsDetail: function(e) {
    wx.navigateTo({
      url: '/pages/goodsdetail/goodsdetail?goodsid=' + e.currentTarget.dataset.id
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    var that = this
    shihan.sendRequest(config.fans_product + that.data.partner_id + '.html', 'GET', {})
      .then(function(response) {
        // console.log(response)
        if (response.data.code == 0) {
          that.setData({
            fans_product: response.data.data,
          })
        }
      }, function(error) {
        console.log(error);
      });
    that.refreshfavoritelist()
  },
  refreshfavoritelist: function() {
    var that = this
    shihan.sendRequest(config.partner_product_rem, 'POST', {
        page: that.data.page,
        page_size: that.data.page_size
      }, {})
      .then(function(response) {
        if (response.data.code == 0 && response.data.data.sh_items.length > 0) {
          for (var i = 0; i < response.data.data.sh_items.length; i++) {
            var productstr = response.data.data.sh_items[i]
            for (var c in productstr) {
              if (c&&c.indexOf("price") >= 0 && productstr[c]) {
                var pricestr = (JSON.stringify(productstr[c])).split('.')
                if (pricestr.length == 1) {
                  productstr[c] = productstr[c] + '.00'
                }
              }
            }
          }
          that.setData({
            page: that.data.page + 1,
            favoritelist: that.data.favoritelist.concat(response.data.data.sh_items),
            favoritelistflag: true,
            prohasMore: true
          })
        } else {
          that.setData({
            favoritelistflag: false,
            prohasMore: false
          })
        }
      }, function(error) {
        console.log(error);
      });
  },
  onReachBottom: function() {
    var that = this
    if (that.data.favoritelistflag) {
      that.setData({
        favoritelistflag: false
      })
      this.refreshfavoritelist()
    }
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  }
})